package com.example.a117.wangyimusic;

import android.content.Context;
import android.graphics.drawable.Icon;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.transition.Transition;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.WindowManager;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.Switch;
import android.widget.Toast;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity implements View.OnClickListener{



    final String TAG = "MainActivity";
    ImageView videoImg;
    ImageView musicImg;
    ImageView friendsImg;
    List<ImageView> imgList;
    List<Integer> imgSrcList;
    ViewPager viewPager;
    FragmentManager fragmentManager;
    FragmentTransaction transaction;
    List<Fragment> fragmentList;
    Context context;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Toolbar toolbar = (Toolbar) findViewById(R.id.myToolBar);
        setSupportActionBar(toolbar);
        getSupportActionBar().setDisplayShowTitleEnabled(false);
        toolbar.setNavigationIcon(R.mipmap.nav);
        context = MainActivity.this;
        InitActivity();
    }

    /**
     * 屏幕参数
     */
//    public void getAndroiodScreenProperty() {
//        WindowManager wm = (WindowManager) this.getSystemService(Context.WINDOW_SERVICE);
//        DisplayMetrics dm = new DisplayMetrics();
//        wm.getDefaultDisplay().getMetrics(dm);
//        int width = dm.widthPixels;         // 屏幕宽度（像素）
//        int height = dm.heightPixels;       // 屏幕高度（像素）
//        float density = dm.density;         // 屏幕密度（0.75 / 1.0 / 1.5）
//        int densityDpi = dm.densityDpi;     // 屏幕密度dpi（120 / 160 / 240）
//        // 屏幕宽度算法:屏幕宽度（像素）/屏幕密度
//        int screenWidth = (int) (width / density);  // 屏幕宽度(dp)
//        int screenHeight = (int) (height / density);// 屏幕高度(dp)
//
//
//        Log.d("h_bl", "屏幕宽度（像素）：" + width);
//        Log.d("h_bl", "屏幕高度（像素）：" + height);
//        Log.d("h_bl", "屏幕密度（0.75 / 1.0 / 1.5）：" + density);
//        Log.d("h_bl", "屏幕密度dpi（120 / 160 / 240）：" + densityDpi);
//        Log.d("h_bl", "屏幕宽度（dp）：" + screenWidth);
//        Log.d("h_bl", "屏幕高度（dp）：" + screenHeight);
//    }
//
/**
 * 处理fragment界面切换
 */
    public void reFreshFragment(int position){
        Log.d(TAG, "reFreshFragment: ");
        System.out.println("refresh");
        //View view;
        InitMenu();
        ImageView img;
        FragmentTransaction transaction = fragmentManager.beginTransaction();
        switch(position){
            case 0:{
                Log.d(TAG, "reFreshFragment: 0");
                img = (ImageView) findViewById(R.id.video);
                img.setImageResource(R.mipmap.video_white);
                transaction.replace(R.id.main_fragment,new videoFragment());
                transaction.commit();
                Log.d(TAG, "reFreshFragment: transaction.commit()");
                break;
            }
            case 1:{
                Log.d(TAG, "reFreshFragment: 1");
                img = (ImageView) findViewById(R.id.music);
                img.setImageResource(R.mipmap.music_white);
                transaction.replace(R.id.main_fragment,new musicFragment());
                transaction.commit();
                Log.d(TAG, "reFreshFragment: transaction.commit()");
                break;
            }
            case 2:{
                Log.d(TAG, "reFreshFragment: 2");
                img = (ImageView) findViewById(R.id.friends);
                img.setImageResource(R.mipmap.friends_white);
                transaction.replace(R.id.main_fragment,new friendsFragment());
                transaction.commit();
                Log.d(TAG, "reFreshFragment: transaction.commit()");
                break;
            }
            default:
                break;
        }
    }



    /**
     * 初始化活动
     */
    public void InitActivity(){
        videoImg = (ImageView) findViewById(R.id.video);
        musicImg = (ImageView) findViewById(R.id.music);
        friendsImg = (ImageView) findViewById(R.id.friends);
        imgList = new ArrayList<ImageView>();
        imgList.add(videoImg);
        imgList.add(musicImg);
        imgList.add(friendsImg);
        for(ImageView img : imgList){
            img.setOnClickListener(this);
        }
        imgSrcList = new ArrayList<Integer>();
        imgSrcList.add(R.mipmap.video_gray);
        imgSrcList.add(R.mipmap.music_gray);
        imgSrcList.add(R.mipmap.friends_gray);
        viewPager = (ViewPager) findViewById(R.id.main_viewPager);
        fragmentList = new ArrayList<Fragment>();
        fragmentList.add(new videoFragment());
        fragmentList.add(new musicFragment());
        fragmentList.add(new friendsFragment());
        fragmentManager = getSupportFragmentManager();
        viewPager.setAdapter(new MyPagerAdapter(fragmentManager,fragmentList));
        viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                Log.d(TAG, "onPageSelected: ");
                reFreshFragment(position);
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.toolbar,menu);
        return  true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        Log.d(TAG, "onOptionsItemSelected: ");
        switch (item.getItemId()){
            case R.id.search:{
                Toast.makeText(this,"I click search",Toast.LENGTH_SHORT).show();
                break;
            }
        }
        return true;
    }

    /*
    初始化菜单栏按钮图片资源
     */
    private void InitMenu() {
        Log.d(TAG, "InitMenu: ");
        for (int i = 0; i < imgList.size(); i++) {
            ImageView img = imgList.get(i);
            img.setImageResource(imgSrcList.get(i));
        }
    }

    @Override
    public void onClick(View view) {
        Log.d(TAG, "onClick: ");
        switch (view.getId()){
            case R.id.video:{
                reFreshFragment(0);
                break;
            }
            case R.id.music:{
                reFreshFragment(1);
                break;
            }
            case R.id.friends:{
                reFreshFragment(2);
                break;
            }
            default:
                break;

        }
    }
}
